//
//  EditProfileViewController.m
//  Mecca
//
//  Created by Nico Villanueva iOS Dev on 20/8/14.
//  Copyright (c) 2014 TapDash Heuristics. All rights reserved.
//

#import "EditProfileViewController.h"

#import "UserDetails.h"

#import "ParserClass.h"

#import "PostClass.h"

#import "UIManager.h"

#import "UserProfileViewController.h"


@interface EditProfileViewController (){
    
    UIImagePickerController *imgPicker;
}

@end

@implementation EditProfileViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


-(void)viewWillAppear:(BOOL)animated{
    NSLog(@"User Profile ssss");
    
    self.activityloader.hidden = YES;
    
    
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    imgPicker = [[UIImagePickerController alloc] init];
    imgPicker.allowsImageEditing = YES;
    //self.imgPicker.delegate = self;
    imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    //imgarry = [[NSArray alloc]initWithObjects:@"terms.png",@"change-profile.png",nil];
    UIImage* img = [UIImage imageNamed:@""];
    UIImageWriteToSavedPhotosAlbum(img,nil,nil,nil);
    
    UserDetails *uDetails = [[ParserClass sharedItems] userDetails][0];
    
    self.txtName.text = [uDetails dname];
    self.txtContactNumber.text = [uDetails contact];
    self.txtCurrent.text = [uDetails password];
    [self setImageString:[uDetails image]];
    
    
    
    
    
    
    // Do any additional setup after loading the view from its nib.
}


- (void)setImageString:(NSString *)strImage{
    NSString *strImg = [NSString stringWithFormat:@"http://192.185.93.102/~boomeran/backend-panel/%@",strImage];
    
    [NSThread detachNewThreadSelector:@selector(showImage:) toTarget:self withObject:strImg];
}


-(void)showImage:(NSString*)imageShow{
    @autoreleasepool {
        NSData *imageList = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageShow]];
        [self.imgNewProfilePic setAlpha:0];
        [UIView animateWithDuration:0.5 animations:^{
            
            self.imgNewProfilePic.image = [UIImage imageWithData:imageList];
            //[self.actLoading setHidden:YES];
            [self.imgNewProfilePic setAlpha:1];
        } completion:^(BOOL finished) {
            
        }];
        
        
        //[self animationActivity];
    }
}




- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnDone:(id)sender {
    
    self.activityloader.hidden = NO;
    
    NSLog(@"Done");
    
    
    if ([self.txtPassword.text isEqualToString:self.txtConfirmPass.text]) {
        self.lblStatus.text = @"Correct";
        [self.lblStatus setTextColor:[UIColor greenColor]];
        
    }else{
        self.lblStatus.text = @"Password Not Match";
        [self.lblStatus setTextColor:[UIColor redColor]];
    }
    
    UserDetails *uDetails = [[ParserClass sharedItems] userDetails][0];
    
    
    NSDictionary *dictProfile = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[uDetails idUser],@"user_id", self.txtName.text, @"user_dname", self.txtContactNumber.text, @"user_contact", self.txtConfirmPass.text,@"user_password", self.imgNewProfilePic.image, @"image",  nil];
  
    NSString *json = [[PostClass sharedItems] updateProfile:dictProfile];
    
    NSLog(@"json = %@", json);
    
    if ([json isEqualToString:@"1"]) {
        
        
        UIAlertView *alertSuccess = [[UIAlertView alloc] initWithTitle:@"Success" message:@"You Successfully Change Your Details" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        
        [[[UIManager sharedItems] userProfileView] updateSuperView];
        
        
        
        [self.view removeFromSuperview];
        
        [alertSuccess show];
        
    }else{
        
        self.activityloader.hidden = YES;
        
        UIAlertView *alertFailed = [[UIAlertView alloc] initWithTitle:@"Failed" message:@"Failed to Change Your Details" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        
        [alertFailed show];
    }
    
    
    
}


- (IBAction)btnProfileChange:(id)sender {
    
    
    NSLog(@"Click Change image");
    
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    imgPicker.delegate = self;
    imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    [self presentModalViewController:imgPicker animated:YES];
    
}

- (IBAction)btnBack:(id)sender {
    [self.view removeFromSuperview];
}



- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:YES];
    self.imgNewProfilePic.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}


#pragma mark - UITextField Delegate


-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    if (self.view.frame.size.height == 568) {
        
        [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
                         animations:^{
                             self.scroller.contentOffset = CGPointMake(0, 130);
                         }
                         completion:nil
         ];
    }else{
        
        [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
                         animations:^{
                             self.scroller.contentOffset = CGPointMake(0, 180);
                         }
                         completion:nil
         ];
    }
    return YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    if (self.view.frame.size.height == 568) {
        
        [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
                         animations:^{
                             self.scroller.contentOffset = CGPointMake(0, 0);
                             
                         }
                         completion:nil
         ];
        
    }else{
        [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
                         animations:^{
                             self.scroller.contentOffset = CGPointMake(0, 0);
                             
                         }
                         completion:nil
         ];
    }
    
    return YES;
}

@end
